package com.wrl.mmall;

/**
 * 1. 排序
 * Created by Wang Renlei on 2019/5/21.
 */
public class Ranking {

    static int[] arr = {
            35, 94, 37, 05, 73, 94, 723, 846, 34, 8, 65, 87, 36, 698, 1, 21, 19, 7, 4, 9, 13, 17, 31, 0, 5, 515, 363,
            756, 525, 262, 782, 154, 11, 167, 15, 101, 53, 26, 84, 94, 27, 48, 94, 2, 6, 3, 11, 22, 26, 83, 14, 738, 51
    };
    static int len = arr.length;

    public static void main(String[] args) {
        test1(arr, len);
//        test2(arr, len);
    }

    private static void test2(int[] arr, int len) {
        int tmp = arr[0];
        int low = arr[0];
        int high = arr[len-1];
        for (int i = 0; i < len; i++){
            if(arr[len -1] > tmp){

            }
        }

    }

    /**
     * 1.1 选择排序
     * 1）首先遍历整个数组，找到最小的那个数，跟数组的第一个元素交换
     * 2）再次遍历数组，从第二个元素开始遍历，找到最小的元素，与第二个元素比较
     * 3）以此类推
     */
    private static void test1(int[] arr, int n) {
        long bTime = System.currentTimeMillis();
        int count = 0;
        int minIndex = 0;
        for (int i = 0; i < n ; i++) {
            for (int j = i; j < n; j++) {
                if (arr[i] < arr[j]) {
                    count++;
                    minIndex = arr[i];
                    arr[i] = arr[j];
                    arr[j] = minIndex;
                }
            }
        }
        for (int i : arr) {
            System.out.print(i + "\t");
        }
        System.out.println("\n循环了：" + count + "次。耗时：" + (System.currentTimeMillis() - bTime) + "毫秒");
    }


}
